//作业二：
//题目：
const fp = require('lodash/fp');
const cars = [
  {
    name: 'Ferrari FF',
    horsepower: 660,
    dollar_value: 700000,
    in_stock: true,
  },
  {
    name: 'Spyker C12 Zagato',
    horsepower: 650,
    dollar_value: 648000,
    in_stock: false,
  },
  {
    name: 'Jaguar XKR-S',
    horsepower: 550,
    dollar_value: 132000,
    in_stock: false,
  },
  {
    name: 'Audi R8',
    horsepower: 525,
    dollar_value: 114200,
    in_stock: false,
  },
  {
    name: 'Aston Martin One-77',
    horsepower: 750,
    dollar_value: 1850000,
    in_stock: true,
  },
  {
    name: 'Pagani Huayra',
    horsepower: 700,
    dollar_value: 1300000,
    in_stock: false,
  },
];

//practice1:
//title
let isLastInStock = function (cars) {
  let last_car = fp.last(cars);
  return fp.prop('in_stock', last_car);
};
// console.log(isLastInStock(cars));

//myMethod
const getIsLastInStock = fp.flowRight(
  JSON.parse,
  String,
  fp.props('in_stock'),
  fp.last
);
console.log('practice1_result:' + getIsLastInStock(cars));

//practice2:
//myMethod
const getFirstCarName = fp.flowRight(String, fp.props('name'), fp.first);
console.log('practice2_result:' + getFirstCarName(cars));

//practice3:
//title:
let _average = function (xs) {
  return fp.reduce(fp.add, 0, xs) / xs.length;
};
//myMethod
let averageDollarValue = function (cars) {
  let dollar_values = fp.map(function (car) {
    return car.dollar_value;
  }, cars);
  return _average(dollar_values);
};
// console.log(averageDollarValue(cars));

//myMethod
const get_average = fp.flowRight(
  _average,
  fp.map((ele) => ele.dollar_value)
);
console.log('practice3_result:' + get_average(cars));

//practice4:
//title:
let _underscore = fp.replace(/\W+/g, '_');

//myMethod
const sanitizeNames = fp.flowRight(
  fp.map((ele) => {
    return fp.flowRight(fp.toLower, _underscore)(ele);
  }),
  fp.map((ele) => ele.name)
);
console.log('practice4_result:', sanitizeNames(cars));
